package com.shuoyue.ycgk.ui.papergroups;

import com.shuoyue.ycgk.base.BaseModel;
import com.shuoyue.ycgk.base.BasePresenter;
import com.shuoyue.ycgk.base.BaseResult;
import com.shuoyue.ycgk.base.BaseView;
import com.shuoyue.ycgk.entity.UserTestPaper;
import com.shuoyue.ycgk.entity.SpecialType;
import com.shuoyue.ycgk.net.RetrofitClient;
import com.shuoyue.ycgk.net.base.ApiSubscriber;
import com.shuoyue.ycgk.net.gsondefaultadapter.Optional;

import java.util.List;

import io.reactivex.Observable;

public interface SpecialQuestionContract {
    interface View extends BaseView {
        /**
         * 专项列表
         *
         * @param specialTypeList
         */
        void setTypeList(List<SpecialType> specialTypeList);

        /**
         * 生成用户试卷
         *
         * @param name
         * @param specialPager
         */
        void setPagerInfo(String name, UserTestPaper specialPager);
    }

    class Presenter extends BasePresenter<View> {
        Model model = new Model();

        public void getSpecialList(int moduleId, int subjectId) {
            apply(model.getSpecials(moduleId, subjectId))
                    .subscribe(new ApiSubscriber<Optional<List<SpecialType>>>(mView, this) {
                        @Override
                        public void onNext(Optional<List<SpecialType>> listOptional) {
                            super.onNext(listOptional);
                            mView.setTypeList(listOptional.getIncludeNull());
                        }
                    });
        }

        public void createSpecialPager(String name, int chapterId, int sectionId, int epId) {
            //test  483
//            chapterId = 483;
//            sectionId = 0;
//            epId = 0;
            apply(model.createSpecialPager(name, chapterId,
                    sectionId == 0 ? null : sectionId,
                    epId == 0 ? null : epId))
                    .subscribe(new ApiSubscriber<Optional<UserTestPaper>>(mView, this) {
                        @Override
                        public void onNext(Optional<UserTestPaper> specialPagerOptional) {
                            super.onNext(specialPagerOptional);
                            UserTestPaper specialPager = specialPagerOptional.getIncludeNull();
                            specialPager.setChapterId(chapterId);
                            specialPager.setSectionId(sectionId);
                            specialPager.setEpId(epId);
                            specialPager.setName(name);
                            mView.setPagerInfo(name, specialPagerOptional.getIncludeNull());
                        }
                    });
        }
    }

    class Model extends BaseModel {
        Observable<BaseResult<List<SpecialType>>> getSpecials(int moduleId, int subjectId) {
            return RetrofitClient.getInstance().getApi().getSpecialList(moduleId, subjectId);
        }

        Observable<BaseResult<UserTestPaper>> createSpecialPager(String name,
                                                                 Integer chapterId,
                                                                 Integer sectionId,
                                                                 Integer epId) {
            return RetrofitClient.getInstance().getApi().createSpecialPager(name, chapterId, sectionId, epId);
        }
    }
}
